package io.renren.modules.report.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.job.entity.ScheduleJobLogEntity;
import io.renren.modules.report.entity.PackageUserEntity;
import io.renren.modules.report.service.PackageUserService;
import io.renren.modules.report.vo.PackageUserVO;
import io.renren.modules.report.vo.SelectedVO;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.report.entity.BusinessPackageEntity;
import io.renren.modules.report.service.BusinessPackageService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 业务包表
 *
 * @author chenshun
 * @email 379249906@qq.com
 * @date 2020-07-07 17:47:36
 */
@RestController
@RequestMapping("report/businesspackage")
public class BusinessPackageController {
    @Autowired
    private BusinessPackageService businessPackageService;

    @Autowired
    private PackageUserService packageUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessPackageService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 查看他所拥有的业务包
     * @param packageId
     * @return
     */
    @RequestMapping("/list/{userId}")
    public R packageList(@PathVariable("userId") Long userId){
        List<PackageUserEntity> packageUserEntities = packageUserService.list(new QueryWrapper<PackageUserEntity>().eq("user_id", userId));

        if(packageUserEntities.size()==0){
            return R.ok().put("packageList", null);
        }

        List<Long> ids = packageUserEntities.stream().map(PackageUserEntity::getPackageId).collect(Collectors.toList());

        if(ids.size()==0){
            return R.ok().put("packageList", null);
        }
        List<BusinessPackageEntity> businessPackageEntities = businessPackageService.listByIds(ids);


        List<SelectedVO> collect = businessPackageEntities.stream().map(attr -> {
            SelectedVO selectedVO = new SelectedVO();
            selectedVO.setLabel(attr.getName());
            selectedVO.setValue(attr.getId());
            return selectedVO;
        }).collect(Collectors.toList());


        return R.ok().put("packageList", collect);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		BusinessPackageEntity businessPackage = businessPackageService.getById(id);

        return R.ok().put("businessPackage", businessPackage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PackageUserVO packageUserVO){

        businessPackageService.saveCascade(packageUserVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PackageUserVO packageUserVO){
        businessPackageService.updateCascade(packageUserVO);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        businessPackageService.deleteCascade(ids);
        return R.ok();
    }

}
